/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * WebSocketConfiguration.java
 * netsystem
 * 2013年10月31日下午12:18:26
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.config;

import java.io.File;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.jackysoft.bean.ServicesLaucher;
import org.jackysoft.net.update.UpdateServer;
import org.jackysoft.net.update.UpdateServerHandler;
import org.jackysoft.service.impl.UserNotificationService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import com.corundumstudio.socketio.SocketIOServer;


@Configuration
public class NetWorkConfig extends AbstractSpringConfig{
    
	
	static final Log logger = LogFactory.getLog(NetWorkConfig.class);
	
	@Resource
	private ContextConfig context;
	
	
	@Bean
	@DependsOn("remindEventServiceImpl")
	public UserNotificationService userNotificationService(){
		UserNotificationService bean = new UserNotificationService();
		return  bean;
	}

			
	@Bean
	@DependsOn("userNotificationService")
	public SocketIOServer socketIoServer(){
		com.corundumstudio.socketio.Configuration
 config = new com.corundumstudio.socketio.Configuration();
        config.setHostname(context.websocketHost);
        config.setPort(context.websocketPort);
        SocketIOServer server = new SocketIOServer(config);
        server.addListeners(userNotificationService());
        userNotificationService().setServer(server);
        return server;
	}
	
	
	
	@Resource
	private UpdateServerHandler updateHandler;
	
	@Bean
	public UpdateServer  updateServer(){
		UpdateServer bean = new UpdateServer(updateHandler,context.updatePort);
		return bean;
	}
	
	@Bean
	public FtpServer ftpServer(){
		 FtpServerFactory serverFactory = new FtpServerFactory();
	        
	        ListenerFactory factory = new ListenerFactory();
	        
	        // set the port of the listener
	        factory.setPort(context.ftpport);

	        // define SSL configuration
	        SslConfigurationFactory ssl = new SslConfigurationFactory();
	        ssl.setKeystoreFile(new File("ftpserver.jks"));
	        ssl.setKeystorePassword("password");

	        // set the SSL configuration for the listener
	        factory.setSslConfiguration(ssl.createSslConfiguration());
	        factory.setImplicitSsl(true);

	        // replace the default listener
	        serverFactory.addListener("default", factory.createListener());
	       	       
	        PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
	        userManagerFactory.setFile(new File("myusers.properties")); 	        
	        userManagerFactory.setAdminName("admin");	
	        
	        
	        serverFactory.setUserManager(userManagerFactory.createUserManager());
	        
	        
	        // start the server
	        FtpServer server = serverFactory.createServer(); 
	        
	        return server;
	}
	
	
	
	@Bean
	public ServicesLaucher servicesLaucher(){
		ServicesLaucher bean = new ServicesLaucher();
		return bean;
	}
	
	@Bean
	@DependsOn("servicesLaucher")
	public org.jackysoft.service.impl.RemindEventServiceImpl remindEventServiceImpl(){
		org.jackysoft.service.impl.RemindEventServiceImpl bean = new org.jackysoft.service.impl.RemindEventServiceImpl();
		return bean;
	}

}
